package com.example.service;

import com.example.mapper.OrderMapper;
import com.example.mapper.ShoppingCarMapper;
import com.example.mapper.UserMapper;
import com.example.pojo.Order;
import com.example.pojo.ShoppingCar;
import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.ObjectMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.web.bind.annotation.*;

import javax.websocket.server.PathParam;
import java.sql.Time;
import java.util.Date;
import java.util.List;

/**
 * @Description:
 * @author: GaoPeng
 * @date: 2021/5/26 21:40
 */
@RequestMapping("/shoppingCarService")
@RestController
@Service
@CrossOrigin
public class ShoppingCarService {

    @Autowired
    private UserMapper userMapper;

    @Autowired
    private OrderMapper orderMapper;

    @Autowired
    private ShoppingCarMapper shoppingCarMapper;

    @Autowired
    private ObjectMapper jsonMapper;

    /**
     * 将商品添加到购物车
     * @param product_id 产品编号
     * @param product_name 产品名称
     * @param price 单价
     * @param amount 数量
     * @param total_price 总价
     * @param user_id 用户id
     * @return
     */
    @PostMapping("/addToShoppingCar")
    Boolean addToShoppingCar(@RequestParam("product_id") Integer product_id,@RequestParam("product_name") String product_name,
                             @RequestParam("price") Float price,@RequestParam("amount") Integer amount,
                             @RequestParam("total_price") Float total_price,@RequestParam("user_id") Integer user_id){
        ShoppingCar shoppingCar=new ShoppingCar();
        shoppingCar.setProduct_id(product_id);
        shoppingCar.setProduct_name(product_name);
        shoppingCar.setPrice(price);
        shoppingCar.setAmount(amount);
        shoppingCar.setTotal_price(total_price);
        shoppingCar.setUser_id(user_id);
        return shoppingCarMapper.insert(shoppingCar)>0;
    }

    /**
     * 通过用户名获取购物车信息
     * @param username
     * @return
     */
    @GetMapping("/getListShoppingCarByUsername/{username}")
    String getListShoppingCarByUsername(@PathVariable String username){
        try {
            return jsonMapper.writeValueAsString(shoppingCarMapper.selectAllByUser_id(userMapper.selectIdByUsername(username)));
        } catch (JsonProcessingException e) {
            e.printStackTrace();
        }

        return null;
    }

    /**
     * 通过商品id修改amount
     * @param productId
     * @param amount
     * @return
     */
    @PostMapping("/updateAmountByProductId")
    Boolean updateAmountByProductId(@RequestParam("productId")Integer productId,@RequestParam("amount")Integer amount){
        return shoppingCarMapper.updateAmountByProductId(amount,productId)>0;
    }

    /**
     * 通过商品id删除某条购物车数据
     * @param productId
     * @return
     */
    @GetMapping("/deleteShoppingCarByProductId/{productId}")
    Boolean deleteShoppingCarByProductId(@PathVariable Integer productId){
        return shoppingCarMapper.deleteByProductId(productId)>0;
    }

}
